<template>
  <div>
    <div id="head">
      <!-- -->
      组织架构
    </div>
    <div id="se">
      部门名称：<input
        style="height: 30px"
        type="text"
        placeholder="请输入...."
      />
      &nbsp;&nbsp;&nbsp;
      <el-button type="info" title="查询" @click="init">查询</el-button>
      <el-button type="info" title="重置" @click="reset">重置</el-button>
    </div>
    <div id="insert">
      <el-button type="info" title="添加" @click="save(0, '顶级部门')"
        >添加</el-button
      >
    </div>
    <!--表格数据-->
    <el-table
      :data="tableData"
      style="width: 100%"
      stripe
      border
      row-key="id"
      lazy
      :load="load"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        label="序号"
        type="index"
        width="180"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="name"
        label="部门名称"
        width="180"
        header-align="center"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
        header-align="center"
      />
      <el-table-column
        prop="status"
        label="状态"
        width="180"
        header-align="center"
      >
        <template #default="{ row }">
          {{ row.status }}
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="chg(row.id, row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" header-align="center">
        <template #default="{ row }">
          <el-button type="success" @click="save(row.id, row.name)"
            >新增</el-button
          >
          <el-button
            type="warning"
            @click="save(row.parentId, row.parentName, row.id, row.name)"
            >编辑</el-button
          >
          <el-button type="danger" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <SaveOrUpdate
      ref="saveDept"
      :visible="isShow"
      @refresh="init()"
      @close="isShow = false"
    />
  </div>
</template>

<!--vue 组件选项定义-->
<script setup>
import { ref, onBeforeMount } from "vue";
import { Delete, Edit, Plus } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { queryDept, changeDeptStatus, deleteDept } from "@/api/dept.js";
import SaveOrUpdate from "./SaveOrUpdate.vue";

//引用保存组件
const saveDept = ref(null);
//表格数据
const tableData = ref([]);
//是否显示新增用户
const isShow = ref(false);
const deptname = ref("");

function reset() {
  deptname.value = "";
  init();
}

//新增部
function save(pid, pname, id, name) {
  //显示组件
  isShow.value = true;
  //调用组件内的初始方法
  saveDept.value.init(pid, pname, id, name);
}

//方法
async function init() {
  let params = {
    parentId: 0,
    name: deptname.value
  };
  let res = await queryDept(params);
  if (res.code == 200) {
    tableData.value = res.data;
  }
}

//加载子节点函数
async function load(row, treeNode, resolve) {
  let params = {
    parentId: row.id
  };
  let res = await queryDept(params);
  if (res.code == 200) {
    resolve(res.data);
  }
}

//删除
function del(id) {
  ElMessageBox.confirm("你确定要删除吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      //确认删除
      todel(id);
    })
    .catch(() => {});
}

async function todel(id) {
  let res = await deleteDept(id);
  if (res.code == 200) {
    ElMessage({
      message: "删除成功",
      type: "success"
    });
    init(); //重新查询数据
  }
}

async function chg(id, status) {
  console.log(id, "    ", status);
  let res = await changeDeptStatus(id, status);
  if (res.code == 200) {
    ElMessage({
      message: "修改成功",
      type: "success"
    });
    init(); //重新查询数据
  }
}

//生命周期钩子
onBeforeMount(() => {
  init();
});
</script>
<style scoped>
#head {
  font-size: medium;
}
#se {
  height: 60px;
  line-height: 60px;
  background-color: #cccc;
  border: gray solid 1px;
  padding-left: 30px;
}
#insert {
  height: 60px;
  line-height: 60px;
  background-color: #fff;
  padding-left: 1100px;
}
</style>
